require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'


class TC_WritePost_test < Test::Unit::TestCase
  def startup
    $Title = 'Title1'
    $TitleEdit = $Title+' -Edit'
    $NewCategory = 'Category1'
    $NewCategoryEdit =$NewCategory+'Edit'
    $Body='This is to test Posts'
    $Body = $Body + '-Edit'
    $Tags = 'first automated'
    $Tags = $Tags +',Edit'
    $Name = 'Name1'
    $NameEdit = $Name+'-Edit'
    $Date = 'July 29 2008 12:58 PM'
    $MetaDescription = 'meta description'
    $MetaDescriptionEdit =$MetaDescription +'editing changes'
    $MetaKeyWords = 'meta key words'
    $MetaKeyWordsEdit = $MetaKeyWords + '-Edit'
    $Notes = 'This are few notes'
    $NotesEdit = $Notes + 'Edit'
    
    @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
 
 #file1 = File.open("D:/tellitests/graffiti/Graffiti/Web/robots.txt")  
 def test_01
     ie.goto($SITE_URL+'graffiti-admin/')
     sleep 5
   end
     def test_02_verifyLoginLinksExist
      if !(ie.link(:href,$SITE_URL+'login/').exists?)
        ie.goto($SITE_URL + 'graffiti-admin/logout')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    def test_03_login
        ie.goto($SITE_URL+'login/')
        assert(ie.text_field(:id,'UserName').exists?)
        ie.text_field(:id,'UserName').value = $ADMIN_USER
        assert(ie.text_field(:id,'Password').exists?)
        ie.text_field(:id,'Password').value = $ADMIN_PASSWORD
        ie.button(:id,'login').click
        ie.goto($SITE_URL+'graffiti-admin/')
      end
        #~ sleep 5
        #ie.frame(:id,'ctl00_MainRegion_txtContent___Frame').text_field(:id,'ctl00_MainRegion_txtContent_Config').value = $Body
      
   def test_04_writeContent
     
        ie.link(:text,'Write').click
        assert(ie.text_field(:id,/txtTitle$/).exists?)
        ie.text_field(:id,/txtTitle$/).set($Title)
        assert(ie.select_list(:id,/CategoryList$/).exists?)
        ie.select_list(:id,/CategoryList$/).select('[Add New Category]')
        assert(ie.text_field(:id,/newCategory$/).exists?)
        ie.text_field(:id,/newCategory$/).set($NewCategory)
      end
      
      def test_05_navigateToSettings
        ie.ie.Document.parentWindow.execScript("ctl00_MainRegion_TabbedPanes1.SelectTab(ctl00_MainRegion_TabbedPanes1.GetTabAtIndex(1))")
        assert(ie.text_field(:id,/txtTags$/).exists?)
        ie.text_field(:id,/txtTags$/).set($Tags)
        assert(ie.text_field(:id,/txtName$/).exists?)
        ie.text_field(:id,/txtName$/).set($Name)
        assert(ie.text_field(:id,/PublishDate_text$/).exists?)
        ie.text_field(:id,/PublishDate_text$/).set($Date)
        assert(ie.checkbox(:id,/EnableComments$/).exists?)
        ie.checkbox(:id,/EnableComments$/).set
        assert(ie.checkbox(:id,/HomeSortOverride$/).exists?)
        ie.checkbox(:id,/HomeSortOverride$/).clear
        assert(ie.checkbox(:id,/FeaturedSite$/).exists?)
        ie.checkbox(:id,/FeaturedSite$/).set
        assert(ie.checkbox(:id,/FeaturedCategory$/).exists?)
        ie.checkbox(:id,/FeaturedCategory$/).set
        sleep 3
        assert(ie.select_list(:id,/PublishStatus$/).exists?)
        sleep 3
        ie.select_list(:id,/PublishStatus$/).select('Draft')
      end
      
      def test_06_navigateToOptions
        ie.ie.Document.parentWindow.execScript("ctl00_MainRegion_TabbedPanes1.SelectTab(ctl00_MainRegion_TabbedPanes1.GetTabAtIndex(2))")
        sleep 3
        assert(ie.text_field(:id,/txtMetaScription$/).exists?)
        sleep 3
        ie.text_field(:id,/txtMetaScription$/).set($MetaDescription)
        sleep 3
        assert(ie.text_field(:id,/txtKeywords$/).exists?)
        ie.text_field(:id,/txtKeywords$/).set($MetaKeyWords)
        #~ sleep 3
        #~ assert(ie.button(:value,'Select ...').exists?)
        #~ ie.button(:value,'Select ...').click
        #~ sleep 5
        
        #~ ie.link(:text,'Home').click
        sleep 3
        assert(ie.text_field(:id,/txtNotes$/).exists?)
        sleep 3
        ie.text_field(:id,/txtNotes$/).set($Notes)
       end
    def test_07_saveDraft
        assert(ie.button(:value,'Save as Draft').exists?)
        ie.button(:value,'Save as Draft').click
      end
      
    def test_08_publishPost
        ie.goto($SITE_URL+'graffiti-admin/posts/?status=2')
        ie.link(:text,$Title)
        ie.ie.Document.parentWindow.execScript("ctl00_MainRegion_TabbedPanes1.SelectTab(ctl00_MainRegion_TabbedPanes1.GetTabAtIndex(1))")
        sleep 3
        assert(ie.select_list(:id,/PublishStatus$/).exists?)
        sleep 3
        ie.select_list(:id,/PublishStatus$/).select('Published')
      end
      
      def test_09_editPost
        ie.goto($SITE_URL+'graffiti-admin/posts/?status=1')
        assert(ie.table(:id,'postList').exists?)
        ie.link(:text,$Title)
      end
      def test_10_editContent
        assert(ie.text_field(:id,/txtTitle$/).exists?)
        ie.text_field(:id,/txtTitle$/).set($TitleEdit)
        assert(ie.select_list(:id,/CategoryList$/).exists?)
        ie.select_list(:id,/CategoryList$/).select('[Add New Category]')
        assert(ie.text_field(:id,/newCategory$/).exists?)
        ie.text_field(:id,/newCategory$/).set($NewCategoryEdit)
      end
      def test_11_editSettings
         ie.ie.Document.parentWindow.execScript("ctl00_MainRegion_TabbedPanes1.SelectTab(ctl00_MainRegion_TabbedPanes1.GetTabAtIndex(1))")
        assert(ie.text_field(:id,/txtTags$/).exists?)
        ie.text_field(:id,/txtTags$/).set($TagsEdit)
        assert(ie.text_field(:id,/txtName$/).exists?)
        ie.text_field(:id,/txtName$/).set($NameEdit)
        assert(ie.text_field(:id,/PublishDate_text$/).exists?)
        ie.text_field(:id,/PublishDate_text$/).set($Date)
        assert(ie.checkbox(:id,/EnableComments$/).exists?)
        ie.checkbox(:id,/EnableComments$/).set
        assert(ie.checkbox(:id,/HomeSortOverride$/).exists?)
        ie.checkbox(:id,/HomeSortOverride$/).clear
        assert(ie.checkbox(:id,/FeaturedSite$/).exists?)
        ie.checkbox(:id,/FeaturedSite$/).set
        assert(ie.checkbox(:id,/FeaturedCategory$/).exists?)
        ie.checkbox(:id,/FeaturedCategory$/).set
        sleep 3
        assert(ie.select_list(:id,/PublishStatus$/).exists?)
        sleep 3
        ie.select_list(:id,/PublishStatus$/).select('Published')
      end
      def test_12_navigateToEditOptions
        ie.ie.Document.parentWindow.execScript("ctl00_MainRegion_TabbedPanes1.SelectTab(ctl00_MainRegion_TabbedPanes1.GetTabAtIndex(2))")
        sleep 3
        assert(ie.text_field(:id,/txtMetaScription$/).exists?)
        sleep 3
        ie.text_field(:id,/txtMetaScription$/).set($MetaDescriptionEdit)
        sleep 3
        assert(ie.text_field(:id,/txtKeywords$/).exists?)
        ie.text_field(:id,/txtKeywords$/).set($MetaKeyWordsEdit)
        sleep 3
        assert(ie.text_field(:id,/txtNotes$/).exists?)
        sleep 3
        ie.text_field(:id,/txtNotes$/).set($NotesEdit)
      end
      def test_13_navigateToPublish
        assert(ie.button(:value,'Publish').exists?)
        ie.button(:value,'Publish').click
      end
      
    def test_14_deletePost
       ie.goto($SITE_URL+'graffiti-admin/posts/?status=1')
       assert(ie.table(:id,'postList').exists?)
        table1 = ie.table(:id, 'postList')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$TitleEdit).exists?)
              ok =1
               sleep 5
               table1[rowcount1][4].link(:text,'Delete').click
               break
             end   
         rowcount1=rowcount1+1
        end
      end 
      
      def test_15_permanentDeletePost
        ie.goto($SITE_URL+'graffiti-admin/posts/?status=-1')
        assert(ie.table(:id,'postList').exists?)
        table1 = ie.table(:id, 'postList')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$TitleEdit).exists?)
              ok =1
               sleep 5
               table1[rowcount1][4].link(:text,'Delete').click
               break
             end   
         rowcount1=rowcount1+1
        end
      end
      
      def test_16_deleteCategory
        ie.link(:text,'Categories').click
        sleep 5 
        ie.ul(:class, 'listboxes').li(:index,2).link(:text,$NewCategory).click
        ie.ul(:class, 'listboxes').li(:index,3).link(:text,$NewCategoryEdit).click
      end
    
    
      
   
end
